import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'package:im_base/public/global/global.dart';

///头像形状
enum AvatarShape {
  circle,
  standard,
  square,
}

///头像尺寸
class AvatarSize {
  static const double SMALL = 30;

  static const double MEDIUM = 35;

  static const double LARGE = 40;
}

class Avatar extends StatelessWidget {
  /// Create Avatar of all types i,e, square, circle, standard with different sizes.
  const Avatar({Key? key,
    this.child,
    this.backgroundColor,
    this.backgroundImage,
    this.foregroundColor,
    this.radius,
    this.minRadius,
    this.maxRadius,
    this.borderRadius,
    this.shape = AvatarShape.circle,
    this.size = AvatarSize.MEDIUM})
      : assert(radius == null || (minRadius == null && maxRadius == null)),
        super(key: key);

  /// Typically a [Text] widget. If the [CircleAvatar] is to have an image, use [backgroundImage] instead.
  final Widget? child;

  /// use [Color] or [GFColors]. The color with which to fill the circle.
  final Color? backgroundColor;

  /// use [Color] or [GFColors]. The default text color for text in the circle.
  final Color? foregroundColor;

  /// The background image of the circle.
  final ImageProvider? backgroundImage;

  /// The size of the avatar, expressed as the radius (half the diameter).
  final double? radius;

  /// The minimum size of the avatar, expressed as the radius (half the diameter).
  final double? minRadius;

  /// The maximum size of the avatar, expressed as the radius (half the diameter).
  final double? maxRadius;

  /// size of avatar. use [AvatarSize] or [double] i.e, 1.2, small, medium, large etc.
  final double size;

  /// shape of avatar [AvatarShape] i.e, standard, circle, square
  final AvatarShape shape;

  /// border radius to give extra radius for avatar square or standard shape
  /// Not applicable to circleshape
  final BorderRadius? borderRadius;

  // /// The default max if only the min is specified.
  // static const double _defaultMaxRadius = double.infinity;

  double get _minDiameter {
    if (radius == null && minRadius == null && maxRadius == null) {
      return 1.5 * size;
    } else {
      return 2.0 * (radius ?? minRadius ?? 0);
    }
  }

  double get _maxDiameter {
    if (radius == null && minRadius == null && maxRadius == null) {
      return 1.5 * size;
    } else {
      return 2.0 * (radius ?? maxRadius ?? 0);
    }
  }

  BoxShape get _avatarShape {
    if (shape == AvatarShape.circle) {
      return BoxShape.circle;
    } else if (shape == AvatarShape.square) {
      return BoxShape.rectangle;
    } else if (shape == AvatarShape.standard) {
      return BoxShape.rectangle;
    } else {
      return BoxShape.rectangle;
    }
  }

  @override
  Widget build(BuildContext context) {
    final Color? backgroundColor = this.backgroundColor;
    final Color? foregroundColor = this.foregroundColor;
    assert(debugCheckHasMediaQuery(context));
    final ThemeData theme = Theme.of(context);
    Color? effectiveBackgroundColor = backgroundColor;

    final double minDiameter = _minDiameter;
    final double maxDiameter = _maxDiameter;
    return AnimatedContainer(
      width: size,
      height: size,
      constraints: BoxConstraints(
        minHeight: minDiameter,
        minWidth: minDiameter,
        maxWidth: maxDiameter,
        maxHeight: maxDiameter,
      ),
      duration: kThemeChangeDuration,
      decoration: BoxDecoration(
        color: effectiveBackgroundColor,
        image: DecorationImage(
          image: backgroundImage != null
              ? backgroundImage!
              : AssetImage("${Global.IMAGE_PATH}header.png"),
          fit: BoxFit.cover,
        ),
        shape: _avatarShape,
        borderRadius: shape == AvatarShape.circle
            ? null
            : borderRadius == null
            ? BorderRadius.circular(10)
            : borderRadius,
      ),
      child: child == null
          ? null
          : child,
    );
  }
}
